﻿/*    Copyright 2012 Robert Williams
    
    This file is part of FileEventWatcher.

    FileEventWatcher is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    FileEventWatcher is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with FileEventWatcher.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.IO;
using System.Windows.Forms;

namespace FileEventWatcher
{
    public partial class SettingsForm : Form
    {
        public SettingsForm()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Boolean IsError = false;

            if (!Directory.Exists(tbMonDirPath.Text.ToString()))
            {
                lblError.Text = "Bad Path";
                lblError.Visible = true;
                IsError = true;
            }
            else
            {
                lblError.Text = String.Empty;
                lblError.Visible = false;
            }

            if( tbFileMonPath.Text.Length != 0)
                if (!File.Exists(tbFileMonPath.Text.ToString()))
                {
                    lblPath.Text = "Bad File Name";
                    lblPath.Visible = true;
                    IsError = true;
                }
                else
                {
                    lblPath.Text = String.Empty;
                    lblPath.Visible = false;
                }

            if (IsError == true)
                MessageBox.Show("Please fix file errors");

            Application.UserAppDataRegistry.SetValue("ApplicationProductVersion", Application.ProductVersion.ToString());
            Application.UserAppDataRegistry.SetValue("PathToMonitoredDirectory", this.tbMonDirPath.Text.ToString());
            Application.UserAppDataRegistry.SetValue("PathToFileMonitorProgram", this.tbFileMonPath.Text.ToString());
            Application.UserAppDataRegistry.SetValue("FilesToMonitor", this.tbFilter.Text.ToString());
            Application.UserAppDataRegistry.SetValue("MonitorCreatedFiles", cbCreated.Checked==true ? "true" : "false");
            Application.UserAppDataRegistry.SetValue("MonitorChangedFiles", cbChanged.Checked == true ? "true" : "false");
            Application.UserAppDataRegistry.SetValue("MonitorDeletedFiles", cbDeleted.Checked == true ? "true" : "false");
            Application.UserAppDataRegistry.SetValue("MonitorRenamedFiles", cbRenamed.Checked == true ? "true" : "false");

            if (IsError == false)
                this.Close();
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            String sVal = String.Empty;

            try
            {
                tbMonDirPath.Text = Application.UserAppDataRegistry.GetValue("PathToMonitoredDirectory").ToString();
            }
            catch (Exception)
            {
                tbMonDirPath.Text = String.Empty;
            }

            try
            {
                tbFileMonPath.Text = Application.UserAppDataRegistry.GetValue("PathToFileMonitorProgram").ToString();
            }
            catch (Exception)
            {
                tbFileMonPath.Text = String.Empty;
            }

            try
            {
                tbFilter.Text = Application.UserAppDataRegistry.GetValue("FilesToMonitor").ToString();
            }
            catch (Exception)
            {
                tbFilter.Text = String.Empty;
            }

            try
            {
                sVal = Application.UserAppDataRegistry.GetValue("MonitorCreatedFiles").ToString();
                cbCreated.Checked = (sVal.ToLower().Contains("true")) ? true : false;
            }
            catch (Exception)
            {
                cbCreated.Checked = false;
            }

            try
            {
                sVal = Application.UserAppDataRegistry.GetValue("MonitorChangedFiles").ToString();
                cbChanged.Checked = (sVal.ToLower().Contains("true")) ? true : false;
            }
            catch (Exception)
            {
                cbChanged.Checked = false;
            }

            try
            {
                sVal = Application.UserAppDataRegistry.GetValue("MonitorDeletedFiles").ToString();
                cbDeleted.Checked = (sVal.ToLower().Contains("true")) ? true : false;
            }
            catch (Exception)
            {
                cbDeleted.Checked = false;
            }

            try
            {
                sVal = Application.UserAppDataRegistry.GetValue("MonitorRenamedFiles").ToString();
                cbRenamed.Checked = (sVal.ToLower().Contains("true")) ? true : false;
            }
            catch (Exception)
            {
                cbRenamed.Checked = false;
            }
        }

        private void tbMonDirPath_Leave(object sender, EventArgs e)
        {
            if (!Directory.Exists(tbMonDirPath.Text.ToString()))
            {
                lblError.Text = "Bad Path";
                lblError.Visible = true;
            }
            else
                lblError.Text = String.Empty;
        }

        private void tbFileMonPath_Leave(object sender, EventArgs e)
        {
            if (tbFileMonPath.Text.Length != 0) 
                if (!File.Exists(tbFileMonPath.Text.ToString()))
                {
                    lblPath.Text = "Bad File Name";
                    lblPath.Visible = true;
                }
                else
                {
                    lblPath.Text = String.Empty;
                    lblPath.Visible = false;
                }
        }
    }
}
